package com.zy.pro.shop.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zy.core.service.impl.ServiceSupport;
import com.zy.pro.shop.dao.CasesMapper;
import com.zy.pro.shop.dao.PicMapper;
import com.zy.pro.shop.model.Cases;
import com.zy.pro.shop.model.Pic;
import com.zy.pro.shop.service.CasesService;
import com.zy.utils.UUIDHelper;
import com.zy.utils.UploadHelper;
@Service
public class CasesServiceImpl extends ServiceSupport<Cases> implements CasesService {

	@Autowired
    private CasesMapper<Cases> mapper;
	
	@Autowired
    private PicMapper<Pic> picMapper;

	public CasesMapper<Cases> getMapper() {
		return mapper;
	}
	
	@Override
	public void addCasesAndPics(Cases cases, String picPath,
			HttpServletRequest request, Logger logger) throws Exception {
		//当前时间
		Date now = new Date();
		//插入案例状态(0:待上架,1:上架,2:下架,3:删除)
		//cases.setCasesStatus(0);
		cases.setCasesStatus(1);
		//插入案例,返回案例主键
		cases.setCaseCreateTime(now);
		mapper.insert(cases);
		//插入案例图片
		if(picPath.indexOf(",") != -1){
			String[] pics = picPath.split(",");
			for (int i = 0; i < pics.length; i++) {
				insertCasePic(cases,now,pics[i],i+1,request,logger);
			}
		}else{
			insertCasePic(cases,now,picPath,1,request,logger);
		}
	}
	
	@Override
	public void editCasesAndPics(Cases cases, String picPath,
			HttpServletRequest request, Logger logger) throws Exception {
		//查询案例的详情信息(包含图片picList,pic按排序字段排序)
		Cases ca = mapper.selectById(cases.getCaseId());
		//当前时间
		Date now = new Date();
		//根据案例主键批量删除 案例图片
		picMapper.deleteByCaseId(ca.getCaseId());
		
		//再插入案例图片
		if(picPath.indexOf(",") != -1){
			String[] pics = picPath.split(",");
			for (int i = 0; i < pics.length; i++) {
				updateCasePic(ca,now,pics[i],i+1,request,logger);
			}
		}else{
			updateCasePic(ca,now,picPath,1,request,logger);
		}
		
		//更新案例信息
		mapper.updateBySelective(cases);
	}
	

	@Override
	public void addCasesAndPics(Cases cases, String picPath0, String picPath1,
			String picPath2, String picPath3, String picPath4, String picPath5,
			String picPath6, String picPath7, String picPath8, String picPath9,
			HttpServletRequest request,Logger logger)
			throws Exception {
		//当前时间
		Date now = new Date();
		//插入案例状态(1:正常;2:冻结,3:删除)
		cases.setCasesStatus(1);
		//插入案例,返回案例主键
		cases.setCaseCreateTime(now);
		mapper.insert(cases);
		//插入案例图片
		insertCasePic(cases,now,picPath0,1,request,logger);
		insertCasePic(cases,now,picPath1,2,request,logger);
		insertCasePic(cases,now,picPath2,3,request,logger);
		insertCasePic(cases,now,picPath3,4,request,logger);
		insertCasePic(cases,now,picPath4,5,request,logger);
		insertCasePic(cases,now,picPath5,6,request,logger);
		insertCasePic(cases,now,picPath6,7,request,logger);
		insertCasePic(cases,now,picPath7,8,request,logger);
		insertCasePic(cases,now,picPath8,9,request,logger);
		insertCasePic(cases,now,picPath9,10,request,logger);
		
	}
		
	/**
	 * 插入案例图片
	 * @param cases 传入案例信息
	 * @param now 时间
	 * @param picPath 路径
	 * @param sort 排序
	 * @param request
	 * @param logger
	 * @throws Exception
	 */
	public void insertCasePic(Cases cases,Date now,String picPath,int sort,HttpServletRequest request,Logger logger) throws Exception{
		if(!"".equals(picPath) && picPath != null){
			Pic pic = new Pic();
			pic.setPicId(UUIDHelper.getUUID36().toString());
			pic.setPicCaseId(cases.getCaseId());
			pic.setPicSort(sort);
			pic.setPicCreateTime(now);
			//将图片复制到指定位置
			if(picPath.indexOf("yibu") != -1){
				//将异步上传的Logo拷贝到正式路径下面
				String newPath = picPath.replaceAll("yibu", "shopCasePic");
				UploadHelper.copyFile(picPath, newPath, "shopCasePic", request, logger);
				pic.setPicPath(newPath);
			}
			//插入pic
			picMapper.insert(pic);
		}
	}

	@Override
	public void editCasesAndPics(Cases cases, String picPath0, String picPath1,
			String picPath2, String picPath3, String picPath4, String picPath5,
			String picPath6, String picPath7, String picPath8, String picPath9,
			HttpServletRequest request, Logger logger) throws Exception {
		//查询案例的详情信息(包含图片picList,pic按排序字段排序)
		Cases ca = mapper.selectById(cases.getCaseId());
		//当前时间
		Date now = new Date();
		//根据案例主键批量删除 案例图片
		picMapper.deleteByCaseId(ca.getCaseId());
		
		//再插入案例图片
		updateCasePic(ca,now,picPath0,1,request,logger);
		updateCasePic(ca,now,picPath1,2,request,logger);
		updateCasePic(ca,now,picPath2,3,request,logger);
		updateCasePic(ca,now,picPath3,4,request,logger);
		updateCasePic(ca,now,picPath4,5,request,logger);
		updateCasePic(ca,now,picPath5,6,request,logger);
		updateCasePic(ca,now,picPath6,7,request,logger);
		updateCasePic(ca,now,picPath7,8,request,logger);
		updateCasePic(ca,now,picPath8,9,request,logger);
		updateCasePic(ca,now,picPath9,10,request,logger);
		
		//更新案例信息
		mapper.updateBySelective(cases);
	}
	
	/**
	 * 
	 * @param cases 案例的详细信息(包括批量删除之前的图片信息)
	 * @param now 时间
	 * @param picPath 路径
	 * @param sort 排序
	 * @param request
	 * @param logger
	 * @throws Exception
	 */
	public void updateCasePic(Cases cases,Date now,String picPath,int sort,HttpServletRequest request,Logger logger) throws Exception{
		if(!"".equals(picPath) && picPath != null){
			Pic pic = new Pic();
			pic.setPicId(UUIDHelper.getUUID36().toString());
			pic.setPicCaseId(cases.getCaseId());
			pic.setPicSort(sort);
			pic.setPicCreateTime(now);
			//将图片复制到指定位置
			if(picPath.indexOf("yibu") != -1){
				//将异步上传的Logo拷贝到正式路径下面
				String newPath = picPath.replaceAll("yibu", "shopCasePic");
				UploadHelper.copyFile(picPath, newPath, "shopCasePic", request, logger);
				pic.setPicPath(newPath);
				
				//判断是否存在旧的图片路径,进行旧图片删除
				if(cases != null && !cases.getPicList().isEmpty()){
					if(cases.getPicList().size() > (sort - 1) && cases.getPicList().get(sort - 1) != null ){
						//删除旧的图片
						UploadHelper.deletePic(cases.getPicList().get(sort - 1).getPicPath(), "shopCasePic", request, logger);
					}
				}
			}else{
				pic.setPicPath(picPath);
			}
			//插入pic
			picMapper.insert(pic);
		}
	}

	/**
	 * 蒋查询出来的案例图片,按排序,补齐10个(按排序排列,空的图片里只放排序序号)
	 */
	@Override
	public Cases findCasesAndAllPics(Integer caseId) throws Exception {
		Cases cases = mapper.selectById(caseId);
		
		List<Pic> picList = new ArrayList<Pic>();
		//蒋案例图片信息按照排序放置到页面上,方便页面判断
		for (int i = 1; i < 11; i++) {
			Pic pic = findSortPic(cases, i);
			if(pic != null){
				picList.add(pic);
			}else{
				Pic p = new Pic();
				p.setPicSort(i);
				picList.add(p);
			}
		}
		cases.setPicList(picList);
		return cases;
	}
	/**
	 * 根据指定排序序号查询案例图片信息是否存在,不存在返回null
	 * @param cases
	 * @param sort
	 * @return
	 * @throws Exception
	 */
	public Pic findSortPic(Cases cases,Integer sort) throws Exception{
		Pic pic = null;
		if(cases != null && !cases.getPicList().isEmpty()){
			a:for (Pic p : cases.getPicList()) {
				if(p.getPicSort() == sort){
					pic = p;
					break a;
				}
			}
		}
		return pic;
	}


}
